import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/HomeView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
      meta: { guest: true },
    },
    {
      path: '/scenic-management',
      name: 'scenic-management',
      component: () => import('@/views/ScenicPointManagementView.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
    },
    {
      path: '/scenic-point-crud',
      name: 'scenic-point-crud',
      component: () => import('@/views/ScenicPointCRUDView.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
    },
    {
      path: '/scenic-browse',
      name: 'scenic-browse',
      component: () => import('@/views/ScenicAreaGuideView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/scenic-recommend',
      name: 'scenic-recommend',
      component: () => import('@/views/ScenicPointRecommendView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/scenic-navigation',
      name: 'scenic-navigation',
      component: () => import('@/views/ScenicPointNavigationView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/maintenance-report',
      name: 'maintenance-report',
      component: () => import('@/views/ScenicPointMaintenanceReportView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/maintenance-records',
      name: 'maintenance-records',
      component: () => import('@/views/ScenicPointMaintenanceRecordsView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/maintenance-management',
      name: 'maintenance-management',
      component: () => import('@/views/ScenicPointMaintenanceManagementView.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
    },
    {
      path: '/scientific-research',
      name: 'scientific-research',
      component: () => import('@/views/ScientificResearchView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/environmental-education',
      name: 'environmental-education',
      component: () => import('@/views/EnvironmentalEducationView.vue'),
      meta: { requiresAuth: true },
    },

    {
      path: '/:pathMatch(.*)*',
      redirect: '/',
    },
  ],
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 需要登录的路由
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否已登录
    if (!userStore.isLoggedIn) {
      // 未登录，重定向到登录页
      next({ name: 'login' })
      return
    }

    // 检查是否需要管理员权限
    if (to.matched.some(record => record.meta.requiresAdmin) &&
        !userStore.isAdmin) {
      // 非管理员访问管理页面，重定向到首页
      ElMessage.warning('您没有权限访问该页面')
      next({ name: 'home' })
      return
    }

    // 已登录且权限正确，允许访问
    next()
  } else if (to.matched.some(record => record.meta.guest)) {
    // 游客页面（如登录页）
    if (userStore.isLoggedIn) {
      // 已登录用户访问登录页，重定向到首页
      next({ name: 'home' })
    } else {
      // 未登录，允许访问
      next()
    }
  } else {
    // 其他页面，允许访问
    next()
  }
})

export default router
